package dk.pun.dums.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;

public class TopBarViewImpl extends Composite implements TopBarView {

	private static TopBarViewUiBinder uiBinder = GWT.create(TopBarViewUiBinder.class);

	@UiTemplate("TopBarView.ui.xml")
	interface TopBarViewUiBinder extends UiBinder<Widget, TopBarViewImpl> { }
	
	@UiField protected transient Anchor anonymousUseAnchor;
	@UiField protected transient Anchor signInAnchor;
	@UiField protected transient Anchor signOutAnchor;
	@UiField protected transient Anchor userNicknameAnchor;

	private transient Presenter presenter;
	
	public TopBarViewImpl() {
		super();
		initWidget(uiBinder.createAndBindUi(this));
	}

	public void setSignedOut() {
		this.anonymousUseAnchor.setVisible(true);
		this.signInAnchor.setVisible(true);
		this.signOutAnchor.setVisible(false);
		this.userNicknameAnchor.setVisible(false);
		
	}

	public void setSignedIn(final String userNickname) {
		if (userNickname != null) {
			this.userNicknameAnchor.setText(userNickname);
		}
		
		this.anonymousUseAnchor.setVisible(false);
		this.signInAnchor.setVisible(false);
		this.signOutAnchor.setVisible(true);
		this.userNicknameAnchor.setVisible(true);
	}

	@Override
	public void setPresenter(final Presenter presenter) {
		this.presenter = presenter;
	}	

	@UiHandler("anonymousUseAnchor")
	public void anonymousButtonClicked(final ClickEvent event) {
		this.presenter.onAnonymousUse();
	}

	@UiHandler("signInAnchor")
	public void signInClickHandler(final ClickEvent event) {
		this.presenter.onSignIn();
	}
	
	@UiHandler("signOutAnchor")
	public void signOutClickHandler(final ClickEvent event) {
		this.presenter.onSignOut();
	}
	
	@UiHandler("userNicknameAnchor")
	public void userNicknameClickHandler(final ClickEvent event) {
		this.presenter.onUsername();
	}
}
